package com.honey.mybatis.generator.mappper;

import com.honey.compilation.xml.Document;
import com.honey.compilation.xml.XmlElement;
import com.honey.core.dbmapping.introspect.IntrospectSchema;
import com.honey.core.dbmapping.introspect.IntrospectSchemaColumn;
import com.honey.core.dbmapping.structure.Schema;
import com.honey.core.types.FullyQualifiedJavaType;
import com.honey.mybatis.generator.MybatisPlugin;

public class MybatisMapper  extends AbstractMybatisMapper {

	public MybatisMapper(MybatisPlugin belongPlugin,IntrospectSchema introspectSchema,IntrospectSchemaColumn introspectSchemaColumn) {
		super(belongPlugin, introspectSchema, introspectSchemaColumn);
	}

	public Document getDocument(){
		Schema schema = introspectSchema.getSchema();
		String namespace = super.getInterfaceClassType(schema, null).getFullyQualifiedName();
		
		FullyQualifiedJavaType type = new FullyQualifiedJavaType(mybatisPlugin.getMapperTargetPackage(),getClassName( schema, mybatisPlugin.getEntityClassSuffix())+"$"+mybatisPlugin.getMapperfileSuffix());
		Document answer =  super.createMyBatisDocument(type,namespace);
		XmlElement rootElement = answer.getRootElement();
		appendElement(rootElement, introspectSchema.getSchema(), introspectSchemaColumn);
		return answer ;
	}
	
	private void appendElement(XmlElement parent, Schema schema, IntrospectSchemaColumn introspectSchemaColumn ){
		(new BaseResultMap(mybatisPlugin)).appendElement(parent, schema, introspectSchemaColumn);		
		if(schema.isIncludeBlobColumn() )
			(new BlobsResultMap(mybatisPlugin)).appendElement(parent, schema, introspectSchemaColumn);		
		(new Cache(mybatisPlugin)).appendElement(parent, schema, introspectSchemaColumn);		
		(new Basefield(mybatisPlugin)).appendElement(parent, schema, introspectSchemaColumn);		
		if(schema.isIncludeBlobColumn() )
			(new Blobfield(mybatisPlugin)).appendElement(parent, schema, introspectSchemaColumn);		
		(new SelectCount(mybatisPlugin)).appendElement(parent, schema, introspectSchemaColumn);		
		(new Exists(mybatisPlugin)).appendElement(parent, schema, introspectSchemaColumn);		
		if(schema.isIncludeBlobColumn() )
			(new SelectByDynamicWithBLOBs(mybatisPlugin)).appendElement(parent, schema, introspectSchemaColumn);		
		(new SelectByDynamic(mybatisPlugin)).appendElement(parent, schema, introspectSchemaColumn);		
		(new SelectByPrimaryKey(mybatisPlugin)).appendElement(parent, schema, introspectSchemaColumn);
		
		if(mybatisPlugin.isEnableDelete() ){
			(new DeleteByPrimaryKey(mybatisPlugin)).appendElement(parent, schema, introspectSchemaColumn);
			(new DeleteByDynamic(mybatisPlugin)).appendElement(parent, schema, introspectSchemaColumn);
		}
		if( mybatisPlugin.isEnableVirtualDeleteKey() ){
			(new VirtualDeleteByPrimaryKey(mybatisPlugin)).appendElement(parent, schema, introspectSchemaColumn);
			(new VirtualDeleteByDynamic(mybatisPlugin)).appendElement(parent, schema, introspectSchemaColumn);
		}
		if(mybatisPlugin.isEnableInsert() ){
			(new Insert(mybatisPlugin)).appendElement(parent, schema, introspectSchemaColumn);
			(new InsertSelective(mybatisPlugin)).appendElement(parent, schema, introspectSchemaColumn);
		}
		if(mybatisPlugin.isEnableUpdate() ){
			(new UpdateByDynamicSelective(mybatisPlugin)).appendElement(parent, schema, introspectSchemaColumn);
			(new UpdateByDynamic(mybatisPlugin)).appendElement(parent, schema, introspectSchemaColumn);
			(new UpdateByPrimaryKeySelective(mybatisPlugin)).appendElement(parent, schema, introspectSchemaColumn);
			(new UpdateByPrimaryKey(mybatisPlugin)).appendElement(parent, schema, introspectSchemaColumn);
		}
	}
}
